package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.common.R;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.util.DigestUtils;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    //登录验证
    @PostMapping("/login")
    public R<Employee> login(HttpSession session, @RequestBody Employee employee){
        //1.页面的密码加密 MD5
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2.查询数据库
        LambdaQueryWrapper<Employee> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername,employee.getUsername());

        Employee emp = employeeService.getOne(lqw);

        //如果没有查询到姓名
        if(emp== null){
            return R.error("登录失败");
        }
        //密码判断
        if(!emp.getPassword().equals(password)){
            return R.error("登录失败");
        }
        //用户状态判断  0:禁用   1:可用
        if(emp.getStatus() == 0){
            return R.error("用户已禁用");
        }
        // 登录成功，将员工id存入Session并返回登录成功结果
        session.setAttribute("employee",emp.getId());
        return R.success(emp);


    }
    //退出登录
    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        session.removeAttribute("employee");
        return R.success("退出成功");

    }

    //添加用户
    @PostMapping
    public R<String> add(HttpServletRequest request,@RequestBody Employee employee){
        log.info("新增员工,员工信息:{}",employee.toString());
        //设置默认的密码 123456 进行md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));



        //获取当前登录用户的id
        //Long empId = (Long) request.getSession().getAttribute("employee");


        employeeService.save(employee);
        return R.success("新增员工成功!");
    }
    //分页查询
   /* page 当前查询页码
    pageSize 每页展示记录数
    name 员工姓名 - 可选参数*/
    @GetMapping("/page")     //page?page=1&pageSize=5&name=??
    public R<Page> page(int page,int pageSize,String name){
        log.info("page = {},pageSize = {},name = {}" ,page,pageSize,name);
        //构建分页查询器
        Page pageinfo =new Page(page,pageSize);

        //条件构造器
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        //添加查询条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        employeeService.page(pageinfo,queryWrapper);
        return R.success(pageinfo);
    }
    //修改数据
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        log.info(employee.toString());
        //获取登录用户id
        //Long empId = (Long) request.getSession().getAttribute("employee");

        employeeService.updateById(employee);

        return R.success("修改员工信息成功!");
    }

    //根据id查询
    @GetMapping("/{id}")
    public R<Employee> getId(@PathVariable Long id){
        log.info("根据员工id进行查询信息");
        Employee employee = employeeService.getById(id);

        if(employee !=null){
            return R.success(employee);
        }
        return R.error("没有查询到对应员工信息");
    }
}
